﻿using Core.Web.Controllers.Queue.AcceptModel;
using Core.Web.Model;
using Core.Framework.Model.Common;
using Core.Framework.Model.ViewModel;
using Core.Framework.Model.WebSockets;
using Core.IBusiness.ISocketModule;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Core.Web.Controllers.Queue
{
    /// <summary>
    /// 任务管理
    /// </summary>
    [Route(".v1/queue/")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Queue)]
    public class QueueController : BaseApiController
    {
        public QueueController(IQueue iQueue) :
            base(iQueue: iQueue)
        { 
        
        }
        
        /// <summary>
        /// 发布任务信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".push")]
        public IResult Push(QueryMessageRequest model)
        {
            if (string.IsNullOrWhiteSpace(model.Token))
                model.Token = Guid.NewGuid().ToString();

            if (string.IsNullOrWhiteSpace(model.Template))
                return new ApiResult { code = CodeResult.PARAMS_IS_NULL, date = "Template not is Null" };

            var entity = new QueryMessage
            {
                Token = model.Token,
                Template = model.Template,
                Parameter = model.Parameter,
                ClientToken = iClientInfo.Token,
                IsMustBeServed = model.IsMustBeServed,
                Content = model.Content,
                MessageKey = model.MessageKey,
                MessageType = model.MessageType,
                SendDateTime = model.SendDateTime
            };

            var result = iQueue.Publish(entity);

            if (result.IsFinished)
            {
                if (result.Date)
                {
                    return new ApiResult { code = CodeResult.SUCCESS, date = model.Token };
                }
                else
                {
                    return new ApiResult { code = CodeResult.QUEUE_CHANNLE_IS_NULL, date = model.Token, info = result.Discription };
                }
                
            }
            else
            {
                return new ApiResult { code = CodeResult.PARAMS_IS_NULL, date = model, info = result.Discription };
            }

        }

        /// <summary>
        /// 调试专用 发布任务信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".testpush")]
        public IResult TestPush(QueryMessageTest model)
        {

            if (string.IsNullOrWhiteSpace(model.Token))
                model.Token = Guid.NewGuid().ToString();

            if (model.SystemToken != CoreStartupHelper.GetConfigValue("Service:System")) {
                return new ApiResult
                {
                    code = CodeResult.PERMISSION_Not_ACCESS
                };
            }


            var result = iQueue.DebugPublish(model);

            if (result.IsFinished)
            {
                if (result.Date)
                {
                    return new ApiResult { code = CodeResult.SUCCESS, date = model.Token };
                }
                else
                {
                    return new ApiResult { code = CodeResult.QUEUE_CHANNLE_IS_NULL, date = model.Token, info = result.Discription };
                }

            }
            else
            {
                return new ApiResult { code = CodeResult.PARAMS_IS_NULL, date = model, info = result.Discription };
            }

        }

        /// <summary>
        /// 收到任务确认
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Confirm")]
        public IResult Confirm(QueueRequest model)
        {
            var result = iQueue.Confirm(model);

            if (result.IsFinished)
            {
                return new ApiResult { code = CodeResult.SUCCESS, date = model };
            }
            else
            {
                return new ApiResult { code = CodeResult.BUSINESS_ERROR, date = model, info = result.Discription };
            }

        }

        /// <summary>
        /// 任务已完成
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Finished")]
        public IResult Finished(QueueRequest model)
        {

            var result = iQueue.Finished(model);

            if (result.IsFinished)
            {
                return new ApiResult { code = CodeResult.SUCCESS, date = model };
            }
            else
            {
                return new ApiResult { code = CodeResult.BUSINESS_ERROR, date = model, info = result.Discription };
            }

        }

        /// <summary>
        /// 任务失败
        /// </summary>
        /// <returns></returns>
        [HttpPost(".Fail")]
        public IResult Fail(QueueRequest model)
        {

            var result = iQueue.Fail(model);

            if (result.IsFinished)
            {
                return new ApiResult { code = CodeResult.SUCCESS, date = model };
            }
            else
            {
                return new ApiResult { code = CodeResult.BUSINESS_ERROR, date = model, info = result.Discription };
            }

        }



        /// <summary>
        /// 订阅渠道
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".Subscribe")]
        public IResult Subscribe(SubscribeRequest model)
        {
            var result = iQueue.Subscribe(iClientInfo.Token,model.channel);

            if (result.IsFinished)
            {
                return new ApiResult { code = CodeResult.SUCCESS, date = $"订阅渠道{model.channel}成功" };
            }
            else
            {
                return new ApiResult { code = CodeResult.BUSINESS_ERROR, date = model, info = result.Discription };
            }

        }


        /// <summary>
        /// 取消订阅渠道
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".UnSubscribe")]
        public IResult UnSubscribe(SubscribeRequest model)
        {
            var result = iQueue.UnSubscribe(iClientInfo.Token, model.channel);

            if (result.IsFinished)
            {
                return new ApiResult { code = CodeResult.SUCCESS, date = $"取消订阅渠道{model.channel}成功" };
            }
            else
            {
                return new ApiResult { code = CodeResult.BUSINESS_ERROR, date = model, info = result.Discription };
            }

        }


    }
}
